package www.xudan.android_ffmpeg.videoplayer.widget;

import android.content.Context;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;

import www.xudan.android_ffmpeg.videoplayer.lifecyclemanager.LifecycleManager;
import www.xudan.android_ffmpeg.videoplayer.render.ResizeTextureView;
import www.xudan.android_ffmpeg.videoplayer.util.AnimUtil;


abstract public class InitByOtherPlayerView extends ControllerView {
    public InitByOtherPlayerView(Context context) {
        super(context);
    }

    public InitByOtherPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public InitByOtherPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected InitByOtherPlayerView parentPlayer;

    public void startWithParent(InitByOtherPlayerView playerView) {
        parentPlayer = playerView;
        playerView.hasChild = true;
        if (player == null) {
            player = playerView.player;
        }
        if (surfaceTexture == null) {
            surfaceTexture = parentPlayer.surfaceTexture;
        }
        textureView = new ResizeTextureView(getContext());
        textureView.setVideoSize(player.getVideoWidth(), player.getVideoHeight());
        textureView.setISurfaceListener(this);
        LayoutParams layoutParams =
                new LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        Gravity.CENTER);
        addView(textureView, 0, layoutParams);
        restoreFromPlayerView(playerView);
        initVolumeAndLuminance();
    }

    protected void restoreFromPlayerView(InitByOtherPlayerView playerView) {
        release = false;
        playerView.status.copyStatusToChild(this);
        player.setOnPreparedListener(this);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setOnCompletionListener(this);
        player.setOnBufferingUpdateListener(this);
        player.setScreenOnWhilePlaying(true);
        player.setOnSeekCompleteListener(this);
        player.setOnErrorListener(this);
        player.setOnInfoListener(this);
        player.setOnVideoSizeChangedListener(this);
        startProgressTimer();
        if (playerView.title != null) {
            setTitle(playerView.title, playerView.alwaysShow);
        }
        status.restoreFromPlayerView();
    }

    protected void startWithChild(InitByOtherPlayerView childPlayer) {
        this.hasChild = false;
        if (player == null) {
            player = childPlayer.player;
        }
        if (surfaceTexture == null) {
            surfaceTexture = childPlayer.surfaceTexture;
        }
        textureView = new ResizeTextureView(getContext());
        textureView.setVideoSize(player.getVideoWidth(), player.getVideoHeight());
        textureView.setISurfaceListener(this);
        LayoutParams layoutParams =
                new LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        Gravity.CENTER);
        addView(textureView, 0, layoutParams);
        restoreFromPlayerView(childPlayer);
        initVolumeAndLuminance();
    }

    protected void childReleasePlayer() {
        release = true;
        removeView(textureView);
        textureView = null;
        cancelProgressTimer();
        AnimUtil.release(this);
        handler.removeMessages(CLOSE_CONTROLLER);
        LifecycleManager.getInstance().unRegisterPlayer(this);
    }

    public void parentReleaseTextureView() {
        cancelProgressTimer();
        removeView(textureView);
        textureView = null;
    }

    @Override
    public void onDestroy() {
        if (release) {
            return;
        }
        if (parentPlayer != null) {
            player = null;
            surfaceTexture = null;
            childReleasePlayer();
            parentPlayer.startWithChild(this);
        } else {
            super.onDestroy();
        }
    }
}
